﻿Public Class frmProspectSearch
    Public pCD_CLI As Integer
    Public pNom As String

    Private szSQL As String
    Private rs As ADODB.Recordset
    Private szCritere As String

    Private Sub frmProspectSearch_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If pCD_CLI = 0 Then
            ' afiche les 50 derniers ELEVES crées par l'agence
            szCritere = "where agence = '" & gUserAgence & "' and cloture_le IS NULL ORDER BY modif_le DESC LIMIT 50"
        Else
            ' affiche les 50 ELEVES à partir de l'élève courant
            szCritere = " WHERE cd_prospect >= " & pCD_CLI & " and cloture_le IS NULL ORDER BY cd_prospect LIMIT 50"
        End If
        InitListeEleves()

    End Sub

    Private Sub InitListeEleves()

        Dim nb As Integer = 0

        ' effacer la listview
        LV_PROSPECTS.Items.Clear()

        ' Lire les 100 derniers ELEVES de l'agence
        szSQL = "SELECT cd_prospect, civilite, nomprenom, permis_demande FROM prospects " & szCritere
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_PROSPECTS.Items.Add(rs.Fields("cd_prospect").Value)
            LV_PROSPECTS.Items(nb).SubItems.Add(rs.Fields("civilite").Value)
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("nomprenom").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("permis_demande").Value))
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()
        'positionner le curseur sur la première ligne
        setIndice(0, LV_PROSPECTS)

    End Sub

    Private Sub LV_PROSPECTS_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_PROSPECTS.DoubleClick
        If LV_PROSPECTS.SelectedItems.Count > 0 Then
            ' mémoriser l'élève sélectionné
            pCD_CLI = LV_PROSPECTS.SelectedItems(0).Text
            pNom = LV_PROSPECTS.SelectedItems(0).SubItems(1).Text & " " & LV_PROSPECTS.SelectedItems(0).SubItems(2).Text
            ' puis retour à l'appelant
            Me.Close()
        End If
    End Sub

    Private Sub LV_PROSPECTS_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_PROSPECTS.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' selection d'un prsoepect
            LV_PROSPECTS_DoubleClick(sender, e)
        End If
    End Sub

    Private Sub NewSearch()
        ' un code ou nom élève est saisi ?
        If TE_NOM.TextLength Then
            If IsNumeric(TE_NOM.Text) Then
                ' si numérique, afiche les 50 ELEVES suivant le code saisi
                szCritere = "where cd_prospect >= " & nq(TE_NOM.Text) & " ORDER BY cd_prospect LIMIT 50"
            Else
                ' si du texte, afiche les 50 ELEVES suivant le nom
                szCritere = "where nomprenom like '" & nq(TE_NOM.Text) & "%' ORDER BY nomprenom LIMIT 50"
            End If
            ' affiche la listview
            InitListeEleves()
        End If
    End Sub

    Private Sub TE_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown

        If e.KeyCode = Keys.Enter Then
            ' nouvelle recherche
            NewSearch()
        End If

    End Sub
End Class